/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.io;

import java.io.IOException;
import java.io.Writer;

import org.apache.commons.collections.CollectionUtils;
import org.jfab.core.collection.CommentList;

/**
 * Provides basic functionality for a code generator.
 */
public final class DefaultBasicCodeGenerator implements BasicCodeGenerator
{
    /** Member variable prefix. */
    private String _memberVariablePrefix = "_";

    @Override
    public final String getMemberVariablePrefix()
    {
        return _memberVariablePrefix;
    }

    @Override
    public final void setMemberVariablePrefix(final String memberVariablePrefix)
    {
        _memberVariablePrefix = memberVariablePrefix;
    }

    @Override
    public final void writeBlankLine(final Writer writer)
    {
        try
        {
            writer.write(LINE_SEPARATOR);
        }
        catch (final IOException e)
        {
            throw new JavaIOWriterException(e.getMessage(), e);
        }
    }

    @Override
    public final void writeCloseBrace(final Writer writer)
    {
        writeLine(writer, "}");
    }

    @Override
    public final void writeLine(final Writer writer, final String line)
    {
        try
        {
            writer.write(line);
            writer.write(LINE_SEPARATOR);
        }
        catch (final IOException e)
        {
            throw new JavaIOWriterException(e.getMessage(), e);
        }
    }

    @Override
    public final void writeMultilineJavadocComment(final Writer writer,
            final CommentList commentLines)
    {
        if (CollectionUtils.isNotEmpty(commentLines))
        {
            final int size = commentLines.size();

            writeLine(writer, "/**");

            for (int i = 0; i < size; i++)
            {
                writeLine(writer, " * " + commentLines.get(i));
            }

            writeLine(writer, " */");
        }
    }

    @Override
    public final void writeOpenBrace(final Writer writer)
    {
        writeLine(writer, "{");
    }

    @Override
    public final void writeSingleLineComment(final Writer writer,
            final String comment)
    {
        writeLine(writer, "// " + comment);
    }

    @Override
    public final void writeSingleLineJavadocComment(final Writer writer,
            final String comment)
    {
        writeLine(writer, "/** " + comment + " */");
    }
}
